package com.sas.web.panel.articles;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.DateTime;

import com.sas.domain.Article;
import com.sas.domain.Client;
import com.sas.domain.Flow;
import com.sas.domain.Vendor;
import com.sas.repository.FlowRepository;
import com.sas.repository.VendorRepository;

public class EnterAmountPanel extends Panel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8321868235064237505L;
	private transient Integer amount = 0;
	private transient double price;
	private transient double finalPrice;
	private transient double discount = 0.0;
	private transient double surcharge = 0.0;
	
	@SpringBean
	private FlowRepository flowRepository;
	
	@SpringBean
	private VendorRepository vendorRepository;
	
	private IModel<Client> clientModel;

	public EnterAmountPanel(String id, final ModalWindow modal1, final IModel<Article> articleModel) {
		super(id);
		final FeedbackPanel feedback = new FeedbackPanel("feedback");
		feedback.setOutputMarkupId(true);
		add(feedback);
		price = articleModel.getObject().getPriceSell();
		finalPrice = price + price*surcharge - price*discount;
		
		
		final Form<Void> form = new Form<Void>("form", new CompoundPropertyModel<Void>(this));
		form.add(new Label("price", Double.toString(price)));
		form.add(new Label("finalPrice", Double.toString(finalPrice)));
		form.add(new TextField<Integer>("amount").setRequired(true));
		form.add(new TextField<Double>("discount"));
		form.add(new TextField<Double>("surcharge"));
		
		
		// label price, label finalprice, texfield discount, texfield surcharge
		// boton de calcular
		
		form.add(new Button("calculateButton") {
			@Override
			public void onSubmit() {
				finalPrice = (price + discount*price + surcharge*price)*amount;
				form.addOrReplace(new Label("finalPrice", Double.toString(finalPrice)));
			}
			
		});
		
		
		form.add(new AjaxButton("sellButton", new ResourceModel("accept"), form)
        {
            /**
			 * 
			 */
			private static final long serialVersionUID = 1533687325827037164L;

			@Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form)
            {
                // repaint the feedback panel so that it is hidden
                target.addComponent(feedback);
				modal1.close(target);
				Article art = articleModel.getObject();
//				Vendor vendor = vendorRepository.find(1);
				
				Flow flow = new Flow(new DateTime(), Flow.FlowType.SALE, 
						art, art.getPriceSell(), amount, finalPrice, discount, surcharge, clientModel.getObject(), null);
				
				flowRepository.store(flow);
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form)
            {
                // repaint the feedback panel so errors are shown
                target.addComponent(feedback);
            }
        });
		
		add(form);
		setVisible(true);
	}

	public void setClientModel(IModel<Client> modelObject) {
		this.clientModel = modelObject;
		
	}

	public Integer getAmount() {
		return amount;
	}

	public void setAmount(Integer amount) {
		this.amount = amount;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public double getFinalPrice() {
		return finalPrice;
	}

	public void setFinalPrice(double finalPrice) {
		this.finalPrice = finalPrice;
	}

	public double getDiscount() {
		return discount;
	}

	public void setDiscount(double discount) {
		this.discount = discount;
	}

	public double getSurcharge() {
		return surcharge;
	}

	public void setSurcharge(double surcharge) {
		this.surcharge = surcharge;
	}
	
	

}

